package net.zjitc;

import net.zjitc.ex01.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/11/1.
 */
public class Test {
    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Person person = new Person();
        Class p = person.getClass();
        //构造方法
        Constructor[] constructors = p.getConstructors();
        for (Constructor c:constructors) {
            System.out.println(c);
        }
        System.out.println("---------------------------------");
        //成员对象
        Field[] fields = p.getDeclaredFields();//获得所有成员变量 ; p.getFields();//获得public成员变量
        for (Field f:fields) {
            f.setAccessible(true);//可修改私有的
            if(f.getName().equals("name")){
                f.set(person,"张三");
            }
            if(f.getName().equals("age")){
                f.set(person,21);
            }
        }
        System.out.println(person);
        System.out.println("---------------------------------");

        //方法对象
        Method[] methods = p.getMethods();
        for (Method m:methods) {
            System.out.println(m);
        }
        //使用方法对象
        Method speck = p.getMethod("speck",String.class,int.class);
        speck.invoke(person,"lisi",30);

    }
}
